import React from 'react';
import {Toast} from 'antd-mobile'
import { connect } from 'react-redux';

import { getEndStep, getManualEnd } from '../common/service'
import { saveData } from '../action/UserAction'
import NoPowerShow from '../component/NoPowerShow'

class CompeleteConstruction extends React.Component {
	constructor(props){
		super(props)
		this.state = {
			equipmentSure:[{id: 0,name: '锤子', num: 15},],
			textEquipment: [{value:{id:1,name:'锤子'},label:'锤子'}], //测试数据
			cantConstruction: false,   //是否无法施工
			constructionCarEdit: false,  //施工计划车辆编辑状态
			constructionPeopleEdit: false,  //施工计划人员编辑状态
			constructionPlanEdit: false, //施工计划编辑状态
			defaultPick: {id: '', name: '请选择设备'}, //选择设备的默认提示
			equipmentNum: '', //填写的设备数量

		}
	}
	
	endStep() {
		const { showLoadModal, hideLoadModal } = this.props;
		showLoadModal();
		getEndStep(this.props.userReducer.flowId) //流程id
		.then((data) => {
			const isOK = data.data;
			console.log(isOK);
			hideLoadModal();
			if (isOK.success) {
				this.props.dispatch(saveData(isOK.obj));
				this.props.currentPage(isOK.obj.flowBaseInfo.flowProcesses);
			} else {
				Toast.info(isOK.msg, 2);
			}
		})
		.catch((e) => {
			hideLoadModal();
			Toast.info('服务器异常', 2);
		})
	}
	manualEnd() {
		const { showLoadModal, hideLoadModal } = this.props;
			showLoadModal();
			getManualEnd(this.props.userReducer.flowId) //流程id
			.then((data) => {
				const isOK = data.data;
				console.log(isOK);
				hideLoadModal();
				if (isOK.success) {
					this.props.dispatch(saveData(isOK.obj));
					this.props.currentPage(isOK.obj.flowBaseInfo.flowProcesses);
				} else {
					Toast.info(isOK.msg, 2);
				}
			})
			.catch((e) => {
				hideLoadModal();
				Toast.info('服务器异常', 2);
			})
	}

	render() {
		//判断是不是当前节点
		if (this.props.currentObj.flowId !== 15 || -1 === this.props.userReducer.roles.findIndex((value) => value === 69) || !this.props.isTodayStep()) {
			return (<NoPowerShow id={this.props.currentObj.flowId}></NoPowerShow>);
		}
		return (
			<div style={{padding: '4.68rem 0.2rem 0 0.2rem'}}>
				<div 
					id='CompeleteConstructionBtn'
					className='btnCommit'
					onTouchStart={()=>{document.getElementById('CompeleteConstructionBtn').style.backgroundColor = '#D97442'}}
					onTouchEnd={()=>{document.getElementById('CompeleteConstructionBtn').style.backgroundColor = '#FFA24A'}}
					onClick={()=>{
						this.endStep()
					}}
					>
					完成施工
				</div>
			</div>
		);
	}
}


function mapStateToProps(state) {
  const { userReducer } = state;
  return {
    userReducer
  };
}

export default connect(mapStateToProps)(CompeleteConstruction);
